from Apps.Profile.utils.jwt_auth import decode_token
from rest_framework import serializers

from .models import *
from Apps.Profile.serializers import ShowUserSerializer
from Apps.Profile.models import User


class CategorySerializer(serializers.ModelSerializer):
    """
    分类
    """

    class Meta:
        model = Category
        fields = "__all__"


class TagSerializer(serializers.ModelSerializer):
    """
    标签
    """

    class Meta:
        model = Tag
        fields = "__all__"


class ArticleSerializer(serializers.ModelSerializer):
    """
    文章
    """
    category = CategorySerializer()
    author = ShowUserSerializer()
    tag = TagSerializer()

    # comment = serializers.SerializerMethodField()
    #
    # def get_comment(self, patent):
    #     role_obj_list = patent.Comment.filter('article'= )
    #     ret = []
    #     for item in role_obj_list:
    #         ret.append({'id': item.id, 'title': item.title})
    #     return ret

    class Meta:
        model = Article
        fields = ['id', 'title', 'body', 'created_time', 'modified_time', 'excerpt', 'category', 'tag', 'author']


class CreateArticleSerializer(serializers.ModelSerializer):
    # category = serializers.CharField(source="get_category_display")
    # tag = serializers.CharField(source="get_tag_display")
    author = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )

    class Meta:
        model = Article
        fields = ("title", "body", "category", "tag", "author")


class CommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment
        fields = '__all__'
